package rs.in.idemdo;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Vector;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import beans.CarrierBean;
import beans.MestoBean;
import beans.TripCatBean;
import beans.TripLine;
import beans.TripPartBean;

public class ListaPutovanjaActivity extends Activity {

	private Vector<TripLine> mListaAktivnihPutovanja;
	private ArrayList<HashMap<String, Object>> mListaZaMapiranje = new ArrayList<HashMap<String, Object>>();
	private static String LISTA_U_BUNDLU = "lista_u_bundlu";
	private static String NOVI_TRIP_LINE = "novi_trip_line";

	private static String ZA_LISTU_MAP = "1";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lista_putovanja);

		mockPodaci();// ////////////////////////////////////////////
		// ucitajPutovanja(savedInstanceState);
		adaptirajListView();
	}

	@SuppressWarnings("unchecked")
	private void ucitajPutovanja(Bundle savedInstanceState) {
		TripLine newTripLine = null;
		mListaAktivnihPutovanja = (savedInstanceState == null) ? null
				: (Vector<TripLine>) savedInstanceState
						.getSerializable(LISTA_U_BUNDLU);

		if (mListaAktivnihPutovanja == null) {
			// znaci da smo onda dosli mozda sa glavnog ekrana ili sa izabrane
			// linije
			Bundle extras = getIntent().getExtras();
			newTripLine = (TripLine) (extras != null ? extras
					.getSerializable(NOVI_TRIP_LINE) : null);
		}

		if (newTripLine != null) {
			mListaAktivnihPutovanja.add(newTripLine);
		}

	}

	@Override
	protected void onPause() {
		super.onPause();

	}

	@Override
	protected void onResume() {
		super.onResume();
		adaptirajListView();
	}

	private void adaptirajListView() {

		final String[] from = new String[] { ZA_LISTU_MAP };
		final int[] to = new int[] { R.id.txv_naziv_putovanja };

		final ListView lv = (ListView) findViewById(R.id.lv_lista_putovanja);
		// napuniArrayListu(mListaAktivnihPutovanja);
		lv.setAdapter(new SimpleAdapter(this, mListaZaMapiranje,
				R.layout.lista_putovanja_stavka, from, to));

	}

	private void napuniArrayListu(Vector<TripLine> tripLines) {
		// final lokalne promenljive radi najbrzeg pristupa
		final ArrayList<HashMap<String, Object>> lista = mListaZaMapiranje;
		final int velicina = tripLines.size();

		for (int i = 0; i < velicina; i++) {
			HashMap<String, Object> mapa = new HashMap<String, Object>();
			mapa.put(ZA_LISTU_MAP, tripLines.get(i).getMestoOd().getNaziv()
					+ " -> " + tripLines.get(i).getMestoDo().getNaziv());
			lista.add(mapa);
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putSerializable(LISTA_U_BUNDLU, mListaAktivnihPutovanja);

	}

	private void mockPodaci() {

		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.HOUR, 2);

		this.mListaAktivnihPutovanja = new Vector<TripLine>();

		// MESTA POLASKA I DOLASKA
		MestoBean ns = new MestoBean(1, "NS");
		MestoBean bg = new MestoBean(2, "Bg");
		MestoBean nis = new MestoBean(3, "Nis");
		// KATEGORIJE PREVOZA
		TripCatBean kategorijaBus = new TripCatBean(1, "BUS");
		TripCatBean kategorijaVoz = new TripCatBean(2, "VOZ");
		// PREVOZNICI
		CarrierBean cbLasta = new CarrierBean(1, "Lasta", "adr", "em@il.rs",
				"222 333", kategorijaBus);
		CarrierBean cbNisExpress = new CarrierBean(2, "Nis Express", "adr1",
				"em@il.rs", "4 222 333", kategorijaBus);
		CarrierBean cbZeleznicaSrbije = new CarrierBean(3, "Zeleznice Srbije",
				"adr2", "em@il.rs", "4 222 333", kategorijaVoz);

		// PUTNE DUZI
		// 1-direkt NS-NIS
		// PRVA LINIJA
		TripPartBean putnaDuzNsNis1 = new TripPartBean(1, 100.0, Calendar
				.getInstance().getTime(), cal.getTime(), null, ns, nis, cbLasta);
		Vector<TripPartBean> duziLinijeNsNis1 = new Vector<TripPartBean>();
		duziLinijeNsNis1.add(putnaDuzNsNis1);
		TripLine putnaLinijaNs_Nis = new TripLine(duziLinijeNsNis1);

		this.mListaAktivnihPutovanja.add(putnaLinijaNs_Nis);
		// DRUGA LINIJA
		TripPartBean putnaDuzNsNis2 = new TripPartBean(2, 100.0, Calendar
				.getInstance().getTime(), cal.getTime(), null, ns, nis,
				cbNisExpress);
		Vector<TripPartBean> duziLinijeNsNis2 = new Vector<TripPartBean>();
		duziLinijeNsNis2.add(putnaDuzNsNis2);
		TripLine putnaLinijaNs_Nis2 = new TripLine(duziLinijeNsNis2);

		this.mListaAktivnihPutovanja.add(putnaLinijaNs_Nis2);

		// TRECA LINIJA,
		// DIREKT VOZ NS-NIS
		TripPartBean putnaDuzNsNis3 = new TripPartBean(3, 100.0, Calendar
				.getInstance().getTime(), cal.getTime(), null, ns, nis,
				cbZeleznicaSrbije);
		Vector<TripPartBean> duziLinijeNsNis3 = new Vector<TripPartBean>();
		duziLinijeNsNis3.add(putnaDuzNsNis3);
		TripLine putnaLinijaNs_Nis3 = new TripLine(duziLinijeNsNis3);

		this.mListaAktivnihPutovanja.add(putnaLinijaNs_Nis3);

		// CETVRTA LINIJA
		// PRESEDANJE U BEOGRADU BUS-VOZ
		TripPartBean putnaDuzNsBg = new TripPartBean(4, 70, Calendar
				.getInstance().getTime(), cal.getTime(), null, ns, bg,
				cbNisExpress);
		TripPartBean putnaDuzBgNis = new TripPartBean(5, 90, Calendar
				.getInstance().getTime(), cal.getTime(), null, bg, ns,
				cbZeleznicaSrbije);
		Vector<TripPartBean> duziLinijeNsNis4 = new Vector<TripPartBean>();
		duziLinijeNsNis4.add(putnaDuzNsBg);
		duziLinijeNsNis4.add(putnaDuzBgNis);
		TripLine putnaLinijaNs_Bg_Nis = new TripLine(duziLinijeNsNis4);

		this.mListaAktivnihPutovanja.add(putnaLinijaNs_Bg_Nis);

		// PETA LINIJA
		// PRESEDANJE U BEOGRADU BUS-BUS
		TripPartBean putnaDuzNsBg1 = new TripPartBean(4, 70, Calendar
				.getInstance().getTime(), cal.getTime(), null, ns, bg,
				cbNisExpress);
		TripPartBean putnaDuzBgNis1 = new TripPartBean(5, 90, Calendar
				.getInstance().getTime(), cal.getTime(), null, bg, ns, cbLasta);
		Vector<TripPartBean> duziLinijeNsNis5 = new Vector<TripPartBean>();
		duziLinijeNsNis5.add(putnaDuzNsBg1);
		duziLinijeNsNis5.add(putnaDuzBgNis1);
		TripLine putnaLinijaNs_Bg_Nis1 = new TripLine(duziLinijeNsNis5);

		this.mListaAktivnihPutovanja.add(putnaLinijaNs_Bg_Nis1);

		napuniArrayListu(this.mListaAktivnihPutovanja);
	}

}
